<template>
  <!-- 期初库存记录 -->
  <div class="flexContent">
    <div class="searchBox">

      <el-form class="demo-form-inline" label-position="left" label-width="auto">
        <div class="serFlexBox">
          <div class="inputBox">
            <el-row :gutter="12" style="display: flex;flex-wrap: wrap;">
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="门店名称">
                  <el-select @click.native="getShopEasy" v-model="storeId" popper-class="eloption" clearable
                    style="width: 95%" filterable placeholder="选择门店">
                    <el-option v-for="shop in shopList" :key="shop.id" :value="shop.id" :label="shop.name" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="商品">
                  <el-input v-clearEmoji v-model.trim="name" style="width: 95%" clearable @keyup.enter.native="serchFn"
                    placeholder="助记符/名称/条码" />
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="10" :xl="6">
                <el-form-item label="时间">
                  <el-date-picker clearable v-model="date" style="width: 95%" type="daterange" unlink-panels
                    range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd" />
                </el-form-item>
              </el-col>
            </el-row>
          </div>
          <div class="btnBox">
            <el-button @click.stop="serchFn" class="searchBtn" icon="el-icon-search">搜索</el-button>
            <el-button @click.stop="resetFn" class="resetBtn" icon="el-icon-refresh-right">重置</el-button>
          </div>
        </div>
      </el-form>
    </div>
    <div class="table" id="tableBox" v-height>
      <div style="flex:1;">
        <vxe-table :column-config="{ resizable: true }" border class="tablebox" ref="xTable1"
          :row-config="{ isHover: true }" :scroll-y="{ enabled: true, gt: 21 }" style="width: 100%" height="100%"
          size="medium" align="center">
          <vxe-column type="seq" width="80" title="序号" />
          <vxe-column field="storeName" title="门店名称" :min-width="elTableAdaptationWidth('storeName', inventoryList)" />
          <vxe-column field="commodityName" title="商品名称"
            :min-width="elTableAdaptationWidth('commodityName', inventoryList)" />
          <vxe-column field="oldInventory" title="期初库存(旧值)" min-width="150" />
          <vxe-column field="newInventory" title="期初库存(新值)" min-width="150" />
          <vxe-column field="updateName" title="修改人" min-width="150" />
          <vxe-column title="修改时间" min-width="150">
            <template v-slot="{ row }">
              <span>{{ row.updateTime | parseTime }}</span>
            </template>
          </vxe-column>
        </vxe-table>
      </div>
      <div class="paginationBox">
        <el-pagination :current-page="page.pageNum" background :page-sizes="$pageSizes" :page-size="page.pageSize"
          layout="total,-> , sizes, prev, pager, next, jumper" :total="page.total" @size-change="handleSizeChange"
          @current-change="handleCurrentChange" />
      </div>
    </div>
  </div>
</template>

<script>
import { getSelectInitialLogs, getShopEasy } from '@/api/shop';
export default {
  name: 'OpeningInventory',
  data() {
    return {
      width: '50%',
      title: '新增',
      showDialog: false,
      isSearch: false,
      shopList: [],
      storeName: '',
      storeId: '',
      name: '',
      date: [],
      deviceNum: '',
      endTime: '',
      openTime: '',
      page: {
        pageNum: 1,
        pageSize: 20,
        total: 0
      },
      inventoryList: [],
      posId: ''
    };
  },
  created() {
    this.getSelectInitialLogs();
    // this.getShopEasy();
  },
  methods: {
    async getShopEasy() {
      const res = await getShopEasy();
      if (res.code === 200) {
        console.log(res);
        const result = res.data;
        console.log('shopList', result);
        this.shopList = result.store;
        console.log('shopList', this.shopList);
      } else {
        // this.$message.error(res.message);
      }
    },
    async getSelectInitialLogs() {
      if (!this.isSearch) {
        this.storeId = '';
        this.name = '';
        this.endTime = '';
        this.openTime = '';
      }
      const res = await getSelectInitialLogs({
        ...this.page,
        storeId: this.storeId,
        name: this.name,
        openTime: this.openTime,
        endTime: this.endTime
      });
      if (res.code === 200) {
        console.log(res);
        const result = res.data;
        this.inventoryList = result.list;
        this.page.pageSize = result.pageSize;
        this.page.pageNum = result.pageNum;
        this.page.total = result.total;
        console.log('inventoryList', this.inventoryList);
        const $table = this.$refs.xTable1
        $table.loadData(this.inventoryList);
      } else {
        // this.$message.error(res.message);
      }
    },
    async serchFn() {
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0
      };
      this.isSearch = true;
      this.openTime = this.date?.length ? this.date[0] : '';
      this.endTime = this.date?.length ? this.date[1] : '';
      this.getSelectInitialLogs();
    },
    resetFn() {
      this.endTime = '';
      this.openTime = '';
      this.storeId = '';
      this.name = '';
      this.date = []
      this.page.pageNum = 1;
      this.page.pageSize = 20;
      this.isSearch = false;
      this.getSelectInitialLogs();
    },
    // 获取条数改变
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.page.pageNum = 1;
      console.log(`每页 ${val} 条`);
      this.getSelectInitialLogs();
    },
    // 获取页数改变
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page.pageNum = val;
      this.getSelectInitialLogs();
    }
  }
};
</script>

<style lang="scss" scoped></style>
